package pssose.spring.controller;

import javax.annotation.Resource;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import pssose.spring.entity.TestEntity;
import pssose.spring.service.ITestEntityService;

@Controller
@RequestMapping("testEntity")
public class TestEntityController {
	@Resource
	private ITestEntityService testEntityService;
	
	@RequestMapping(value="count",method=RequestMethod.GET)
	public @ResponseBody int testCount() {
		return testEntityService.countTestEntity();
	}
	@RequestMapping(value="find",method=RequestMethod.GET)
	public @ResponseBody TestEntity testFind(@RequestParam(value="value",required=false) Integer value
			, @RequestParam(value="info",required=false) String info) {
		return testSearch(value,info,null,null).get(0);
	}
	@RequestMapping(value="search",method=RequestMethod.GET)
	public @ResponseBody List<TestEntity> testSearch(@RequestParam(value="value",required=false) Integer value
			, @RequestParam(value="info",required=false) String info
			, @RequestParam(value="dateStart",required=false) Date dateStart
			, @RequestParam(value="dateEnd",required=false) Date dateEnd) {
		TestEntity te=new TestEntity();
		te.setInfo(info);
		te.setValue(value);
		return testEntityService.searchTestEntity(te);
	}
	@RequestMapping(value="{id}",method=RequestMethod.GET)
	public @ResponseBody TestEntity testGet(@PathVariable int id) {
		return testEntityService.getTestEntity(id);
	}
	@SuppressWarnings("unchecked")
	@RequestMapping(method=RequestMethod.GET)
	public @ResponseBody List<TestEntity> testList() {
		return (List<TestEntity>)testEntityService.listTestEntity();
	}
	@RequestMapping(method=RequestMethod.POST,consumes  = "application/json")
	public @ResponseBody void testPost(@RequestBody TestEntity te) {
		testEntityService.saveTestEntity(te);
	}
	@RequestMapping(value="{id}",method=RequestMethod.PUT,consumes  = "application/json")
	public @ResponseBody void testUpdate(@RequestBody TestEntity te,@PathVariable int id) {
		if(id==te.getId())
			testEntityService.updateTestEntity(te);
	}
	@RequestMapping(value="{id}",method=RequestMethod.DELETE)
	public @ResponseBody void testDelete(@PathVariable int id) {
		testEntityService.deleteTestEntity(id);
	}
}